﻿using DLL.MSDM.Entity.Param;
using DLL.NET70.Infrastructure.Prism;
using DLL.NET70.Infrastructure.Prism.MVVM;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace PrismModule.MS.DeviceParam.ViewModels
{
    public class PUC_CardSelectedViewModel : CMVVMBase, IRegionMemberLifetime
    {
        private CMotionCardType _cardType = new CMotionCardType();

        public CMotionCardType CardType
        {
            get { return _cardType; }
            set { SetProperty(ref _cardType, value); }
        }

        //CardNames
        private ObservableCollection<string> _CardNames = new ObservableCollection<string>();

        public ObservableCollection<string> CardNames
        {
            get { return _CardNames; }
            set
            {
                SetProperty(ref _CardNames, value);
            }
        }

        public bool KeepAlive
        {
            get
            {
                //return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
                return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
            }
        }

        private IContainerExtension _container;

        public PUC_CardSelectedViewModel(
            IRegionManager regionManager,
            IEventAggregator eventAggregator,
            IContainerExtension container,
            IApplicationCommands applicationCommands
            ) : base(regionManager, eventAggregator, applicationCommands)
        {
            Title = "控制卡类型";
            Message = "选择控制卡";
            _container = container;
            SelectedChangedCommand = new DelegateCommand(ComboxSelectedChanged);
            InitializedCommand = new DelegateCommand(ComboxInitialized);

            CWR_MotionCardType wrcard = new CWR_MotionCardType();
            wrcard.ReadParam();
            CardType = wrcard.Obj;
            InitializeCardNames(CardType.DeviceType);
        }

        public override void Loaded()
        {
            RegisterSaveCommand();
            base.Loaded();
        }

        public override void UnLoaded()
        {
            UnRegisterSaveCommand();
            base.UnLoaded();
        }

        private void InitializeCardNames(int deviceType)
        {
            /*
             * <ComboBoxItem>六工位配药机器人</ComboBoxItem>
                <ComboBoxItem>四工位配药机器人</ComboBoxItem>
                <ComboBoxItem>双工位配药机器人</ComboBoxItem>
                <ComboBoxItem>剥盖机</ComboBoxItem>
                <ComboBoxItem>水针配药机器人</ComboBoxItem>
                <ComboBoxItem>控制卡测试</ComboBoxItem>
             */
            /*
             * "TMCM6110And1110_USB",
            "TMCM6110And1110_CAN",
            "TMCM1110_USB",
            "TMCM6110_USB",
            "Vince" ,
            "LHEM"
             */
            CardNames.Clear();
            switch (deviceType)
            {
                case 0:
                    {
                        //6工位机器人，只支持
                        //CardType.MotionCardType = 0;
                        CardNames.Add("TMCM6110And1110_USB");
                        CardNames.Add("TMCM6214And1110_USB");
                        break;
                    }
                case 1:
                    {
                        //CardType.MotionCardType = 0;
                        CardNames.Add("Vince_VSMD");
                        CardNames.Add("Vince_TinyCan");
                        break;
                    }

                default:
                    {
                        break;
                    }
            }
        }

        public override void Save()
        {
            CWR_MotionCardType wrcard = new CWR_MotionCardType();
            CMotionCardType card = new CMotionCardType();
            card = CardType;
            wrcard.SaveParam(card);

            LogMessage("运动控制卡类型选择参数保存完成！软件重启生效！");
        }

        public DelegateCommand SelectedChangedCommand { get; private set; }
        public DelegateCommand InitializedCommand { get; private set; }

        private void ComboxSelectedChanged()
        {
            int deviceType = CardType.DeviceType;
            InitializeCardNames(deviceType);
        }

        private void ComboxInitialized()
        {
        }
    }
}